<?php
ob_start();
session_register("cart");
class ProductsController extends AppController {
var $name = 'Products';	
//var $hasone = 'ProductGroups';
var $helpers = array('Html','Ajax','Javascript','Form','Session');
var $paginate = array(
        'limit' =>10,
        'order' => array('Product.name' => 'asc')
    );
var $uses = array('Product','ProductGroup','Provider','Parameter','Comment','ProductGroup','Shop','Customer','ParametersProduct','Paymethod','Order','Orderdetail');
//var $uses = array('Product','Provider');

function index($id=null){
	$this->layout = "home";	
	$this->Product->id = $id;
	$this->set('iProduct',$this->Product->read());
	$this->set('providerList', $this->Provider->find('all'));
	$this->set('productGroup',$this->ProductGroup->find('all'));
	$this->set('param',$this->Parameter->find('all'));
	$this->set('productgroup',$this->ProductGroup->find('all'));
	$this->set('ishop',$this->Shop->find('all'));
	$data = $this->paginate('Product');
    $this->set('products', $data);
	}
function uploadFiles($folder, $formdata, $itemId = null) {  
    // setup dir names absolute and relative  
    $folder_url = WWW_ROOT.$folder;  
    $rel_url = $folder;  
     return $formdata;
    }  

function save(){
	$id= $_REQUEST['id'];
	$name = $_REQUEST['name'];
	$price = $_REQUEST['price'];
	$quantity = $_REQUEST['quantity'];
	//$picture = $_REQUEST['picture'];
	$rating = $_REQUEST['rating'];
	$description = $_REQUEST['description'];
	$shop_id = $_REQUEST['shop_id'];
	$product_group_id = $_REQUEST['product_group_id'];
	$provider_id = $_REQUEST['provider_id'];
	$fileOK = $this->uploadFiles('/img/San_pham/', $this->data['picture']);  
	//$picture = $this->data['picture']; 
	 if (($id != null) && ($id != "")) {
            $this->Product->id = $id;
            $this->Product->set('name', $name);
			$this->Product->set('price',$price);
			$this->Product->set('quantity',$quantity);
			$this->Product->set('picture',$fileOK["name"]);
			$this->Product->set('rating',$rating);
			$this->Product->set('description',$description);
			$this->Product->set('shop_id',$shop_id);
			$this->Product->set('product_group_id',$product_group_id);
			$this->Product->set('provider_id',$provider_id);
			$this->Product->save();
			} else {
            $this->Product->create();
            $this->Product->set('name', $name);
			$this->Product->set('price',$price);
			$this->Product->set('quantity',$quantity);
			$this->Product->set('picture',$fileOK["name"]);
			$this->Product->set('rating',$rating);
			$this->Product->set('description',$description);
			$this->Product->set('shop_id',$shop_id);
			$this->Product->set('product_group_id',$product_group_id);
			$this->Product->set('provider_id',$provider_id);
			$this->Product->saveAll();
			}
       $this->redirect(array("action" => "index"));
    }
	function delete() {
		$id= $_REQUEST['id'];
		if($id !=null && $id !=""){
		$this->Product->delete($id);
		$this->Session->setFlash(__('ban co chac xoa san pham khong', true));
		$this->redirect(array("action"=>"index"));
		}
	}
	function view($id = null) {
        $this->layout = "home";	
        $this->Product->id = $id;
        $this->set('vProduct', $this->Product->read());
        $this->set('pro', $this->Product->find('all'));
        $this->set('providerList', $this->Provider->find('all'));
        $this->set('shop', $this->Shop->find('all'));
        $this->set('parametersp', $this->ParametersProduct->find('all'));
        $this->set('paramsp', $this->Parameter->find('all'));
		$this->set('comments', $this->Comment->find('all'));
		$this->set('customer', $this->Customer->find('all'));
    }
	
//cart
function addtocart($id=NULL) {
	$this->autoRender = false;
	if(!$this->Session->check("cart")) $this->Session->write("cart"); //neu chua co bien cart thi khoi tao bien cart
    if (count($this->Session->read("cart")) > 0) {
        if (array_key_exists($id, $this->Session->read("cart"))) { //kiem tra xem product_id da ton tai chua
            $soluong = $this->_checkKhoHang($id); //check so luong trong kho hang theo id san pham
            if ($_SESSION["cart"][$id] < $soluong) { //neu so luong trong gio hang < so luong trong kho thi cho add
			 $_SESSION["cart"][$id]++;
            } else {
               echo "<script>alert('Xin loi kho hang chung toi khong du!')</script>";
            }
        } else {
            $this->Session->write("cart.{$id}",1);
        }
    }else{
       $this->Session->write("cart.{$id}",1);
    }
	$this->Session->write("tongsanpham",$this->_layTongSanPhamTrongGioHang($this->Session->read("cart")));
	$this->Session->write("tongtien",$this->_layTongGiaSanPhamTrongGioHang($this->Session->read("cart")));
	print_r($this->Session->read("cart"));
	
}

function _layTongSanPhamTrongGioHang($array_cart) {
    $total = 0;
    if(is_array($array_cart)){
        foreach ($array_cart as $id => $qty) {
            $total+=$qty;
        }
    }
    return $total;
}

function _layTongGiaSanPhamTrongGioHang($array_cart) {
$total = 0;
    if(is_array($array_cart)){
        foreach ($array_cart as $id => $qty) {
            $price = $this->_layGiaTungSanPham($id);
            $total+=$price * $qty;
        }
    }
    return $total;
}
function _layGiaTungSanPham($id) {
    $priceProduct = $this->Product->read(NULL,$id);
    $price = $priceProduct['Product']['price'];
    return $price;
}

function _checkKhoHang($id) {
	$slproduct = $this->Product->read(NULL,$id);
    $sl = $slproduct['Product']['quantity'];
    return $sl;
}
function _layThongTinSanPham($id){
	$this->loadModel("Product");
	$wproduct = $this->Product->read(NULL,$id);
	return $wproduct;
	}
function updatecart() {
 $this->autoRender=false;
    foreach ($this->Session->read("cart") as $id => $qty) {
        if ($_POST[$id] <= 0) {
		unset($_SESSION["cart"][$id]);
        } else {
            $soluong = $this->_checkKhoHang($id);
            if ($_POST[$id] < $soluong) {
			$_SESSION["cart"][$id] = $_POST[$id];
            }else {
                echo "<script>alert('Xin loi kho hang chung toi khong du!')</script>";
            }
        }
    }
	$this->Session->write("tongsanpham",$this->_layTongSanPhamTrongGioHang($this->Session->read("cart")));
	$this->Session->write("tongtien",$this->_layTongGiaSanPhamTrongGioHang($this->Session->read("cart")));
	$this->redirect(array('action'=>'viewcart'));
}

function deleteItemCart($id) {
	$this->autoRender= false;
	unset($_SESSION["cart"][$id]);
	$this->redirect(array('action'=>'viewcart'));
}

function deleteCart() {
	$this->autoRender= false;
	unset($_SESSION["cart"]);
	$this->redirect(array('action'=>'index'));
}
function viewcart($id=null){
		$this->layout = "home";	
		$this->Product->id = $id;
		if($this->Session->read("cart")){
		$this->set('viewcarts',$this->Product->find('all'));
		$this->set("ListPayMethod", $this->Paymethod->find("all"));
		}
}

// end cart
}
?>